from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QColor, QImage, QPalette
from PyQt5.QtWidgets import QFrame
from components import *
from modules import *
from .song_info_card import SongInfoCard

class PlayerBar(QFrame):

    volume = 50
    # playSignal = pyqtSignal()
    nextSongSignal = pyqtSignal()
    lastSongSignal = pyqtSignal()
    # volumeChangedSignal = pyqtSignal(int)
    # volumeButtonSignal = pyqtSignal()
    # progressSliderMovedSignal = pyqtSignal(int)

    def __init__(self):
        super().__init__()
        self.setupUi()
        self.initSlider()
        self.initSignal()
        self.initLastPlaying()

    def setupUi(self):

        self.setMinimumSize(QtCore.QSize(0, 90))
        self.setMaximumSize(QtCore.QSize(16777215, 90))
        self.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.setFrameShadow(QtWidgets.QFrame.Raised)
        self.setObjectName("PlayerBar")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setSpacing(10)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.PlayerBarLeft = QtWidgets.QFrame(self)
        self.PlayerBarLeft.setMinimumSize(QtCore.QSize(360, 90))
        self.PlayerBarLeft.setMaximumSize(QtCore.QSize(360, 90))
        self.PlayerBarLeft.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.PlayerBarLeft.setFrameShadow(QtWidgets.QFrame.Raised)
        self.PlayerBarLeft.setObjectName("PlayerBarLeft")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.PlayerBarLeft)
        self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_3.setSpacing(10)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.MusicCover = QtWidgets.QFrame(self.PlayerBarLeft)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(90)
        sizePolicy.setVerticalStretch(90)
        sizePolicy.setHeightForWidth(self.MusicCover.sizePolicy().hasHeightForWidth())
        self.MusicCover.setSizePolicy(sizePolicy)
        self.MusicCover.setMinimumSize(QtCore.QSize(90, 90))
        self.MusicCover.setMaximumSize(QtCore.QSize(90, 90))
        self.MusicCover.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.MusicCover.setFrameShadow(QtWidgets.QFrame.Raised)
        self.MusicCover.setObjectName("MusicCover")
        self.verticalLayout_9 = QtWidgets.QVBoxLayout(self.MusicCover)
        self.verticalLayout_9.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_9.setSpacing(0)
        self.verticalLayout_9.setObjectName("verticalLayout_9")
        self.MusicCoverLabel = QtWidgets.QLabel(self.MusicCover)
        self.MusicCoverLabel.setText("")
        self.MusicCoverLabel.setScaledContents(True)
        self.MusicCoverLabel.setObjectName("MusicCoverLabel")
        self.verticalLayout_9.addWidget(self.MusicCoverLabel)
        self.horizontalLayout_3.addWidget(self.MusicCover)
        self.MusicInfo = SongInfoCard('', '')
        self.horizontalLayout_3.addWidget(self.MusicInfo)
        self.horizontalLayout_2.addWidget(self.PlayerBarLeft)
        self.PlayerMain = QtWidgets.QFrame(self)
        self.PlayerMain.setMinimumSize(QtCore.QSize(500, 0))
        self.PlayerMain.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.PlayerMain.setFrameShadow(QtWidgets.QFrame.Raised)
        self.PlayerMain.setObjectName("PlayerMain")
        self.verticalLayout_7 = QtWidgets.QVBoxLayout(self.PlayerMain)
        self.verticalLayout_7.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_7.setSpacing(0)
        self.verticalLayout_7.setObjectName("verticalLayout_7")
        self.PlayerWidget = QtWidgets.QFrame(self.PlayerMain)
        self.PlayerWidget.setMinimumSize(QtCore.QSize(0, 55))
        self.PlayerWidget.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.PlayerWidget.setFrameShadow(QtWidgets.QFrame.Raised)
        self.PlayerWidget.setObjectName("PlayerWidget")
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.PlayerWidget)
        self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_4.setSpacing(0)
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.PlayerHandleBox = QtWidgets.QFrame(self.PlayerWidget)
        self.PlayerHandleBox.setMinimumSize(QtCore.QSize(400, 0))
        self.PlayerHandleBox.setMaximumSize(QtCore.QSize(400, 16777215))
        self.PlayerHandleBox.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.PlayerHandleBox.setFrameShadow(QtWidgets.QFrame.Raised)
        self.PlayerHandleBox.setObjectName("PlayerHandleBox")
        self.horizontalLayout_7 = QtWidgets.QHBoxLayout(self.PlayerHandleBox)
        self.horizontalLayout_7.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_7.setSpacing(0)
        self.horizontalLayout_7.setObjectName("horizontalLayout_7")
        self.ShuffleButton = QtWidgets.QPushButton(self.PlayerHandleBox)
        self.ShuffleButton.setMinimumSize(QtCore.QSize(36, 36))
        self.ShuffleButton.setMaximumSize(QtCore.QSize(36, 36))
        self.ShuffleButton.setText("")
        icon8 = QtGui.QIcon()
        icon8.addPixmap(QtGui.QPixmap(":/icons/images/icon/shuffle.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.ShuffleButton.setIcon(icon8)
        self.ShuffleButton.setIconSize(QtCore.QSize(25, 25))
        self.ShuffleButton.setObjectName("ShuffleButton")
        self.horizontalLayout_7.addWidget(self.ShuffleButton)
        self.PreButton = QtWidgets.QPushButton(self.PlayerHandleBox)
        self.PreButton.setMinimumSize(QtCore.QSize(36, 36))
        self.PreButton.setMaximumSize(QtCore.QSize(36, 36))
        self.PreButton.setText("")
        icon9 = QtGui.QIcon()
        icon9.addPixmap(QtGui.QPixmap(":/icons/images/icon/play-previous_1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.PreButton.setIcon(icon9)
        self.PreButton.setIconSize(QtCore.QSize(25, 25))
        self.PreButton.setObjectName("PreButton")
        self.horizontalLayout_7.addWidget(self.PreButton)
        self.PlayButton = QtWidgets.QPushButton(self.PlayerHandleBox)
        self.PlayButton.setMinimumSize(QtCore.QSize(50, 50))
        self.PlayButton.setMaximumSize(QtCore.QSize(50, 50))
        self.PlayButton.setText("")
        icon10 = QtGui.QIcon()
        icon10.addPixmap(QtGui.QPixmap(":/icons/images/icon/play_1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.PlayButton.setIcon(icon10)
        self.PlayButton.setIconSize(QtCore.QSize(46, 46))
        self.PlayButton.setObjectName("PlayButton")
        self.horizontalLayout_7.addWidget(self.PlayButton)
        self.NextButton = QtWidgets.QPushButton(self.PlayerHandleBox)
        self.NextButton.setMinimumSize(QtCore.QSize(36, 36))
        self.NextButton.setMaximumSize(QtCore.QSize(36, 36))
        self.NextButton.setText("")
        icon11 = QtGui.QIcon()
        icon11.addPixmap(QtGui.QPixmap(":/icons/images/icon/play-next_1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.NextButton.setIcon(icon11)
        self.NextButton.setIconSize(QtCore.QSize(25, 25))
        self.NextButton.setObjectName("NextButton")
        self.horizontalLayout_7.addWidget(self.NextButton)
        self.RepectButton = QtWidgets.QPushButton(self.PlayerHandleBox)
        self.RepectButton.setMinimumSize(QtCore.QSize(36, 36))
        self.RepectButton.setMaximumSize(QtCore.QSize(36, 36))
        self.RepectButton.setText("")
        icon12 = QtGui.QIcon()
        icon12.addPixmap(QtGui.QPixmap(":/icons/images/icon/repeat_1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.RepectButton.setIcon(icon12)
        self.RepectButton.setIconSize(QtCore.QSize(25, 25))
        self.RepectButton.setObjectName("RepectButton")
        self.horizontalLayout_7.addWidget(self.RepectButton)
        self.horizontalLayout_4.addWidget(self.PlayerHandleBox)
        self.verticalLayout_7.addWidget(self.PlayerWidget)
        self.ProgressBar = QtWidgets.QFrame(self.PlayerMain)
        self.ProgressBar.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.ProgressBar.setFrameShadow(QtWidgets.QFrame.Raised)
        self.ProgressBar.setObjectName("ProgressBar")
        self.horizontalLayout_8 = QtWidgets.QHBoxLayout(self.ProgressBar)
        self.horizontalLayout_8.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_8.setSpacing(0)
        self.horizontalLayout_8.setObjectName("horizontalLayout_8")

        self.CurrentDuration = QtWidgets.QLabel(self.ProgressBar)
        self.CurrentDuration.setMinimumSize(QtCore.QSize(50, 0))
        self.CurrentDuration.setMaximumSize(QtCore.QSize(50, 16777215))
        self.CurrentDuration.setAccessibleName("")
        self.CurrentDuration.setAccessibleDescription("")
        self.CurrentDuration.setAlignment(QtCore.Qt.AlignCenter)
        self.CurrentDuration.setObjectName("CurrentDuration")
        self.horizontalLayout_8.addWidget(self.CurrentDuration)

        self.ProgressSliderBox = QtWidgets.QFrame(self.ProgressBar)
        self.ProgressSliderBox.setMinimumSize(QtCore.QSize(400, 0))
        self.ProgressSliderBox.setMaximumSize(QtCore.QSize(400, 16777215))
        self.ProgressSliderBox.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.ProgressSliderBox.setFrameShadow(QtWidgets.QFrame.Raised)
        self.ProgressSliderBox.setObjectName("ProgressSliderBox")
        self.ProgressSliderLayout = QtWidgets.QVBoxLayout(self.ProgressSliderBox)
        self.ProgressSliderLayout.setContentsMargins(0, 0, 0, 0)
        self.ProgressSliderLayout.setSpacing(0)
        self.ProgressSliderLayout.setObjectName("ProgressSliderLayout")
        self.horizontalLayout_8.addWidget(self.ProgressSliderBox)

        self.TotalDuration = QtWidgets.QLabel(self.ProgressBar)
        self.TotalDuration.setMinimumSize(QtCore.QSize(50, 0))
        self.TotalDuration.setMaximumSize(QtCore.QSize(50, 16777215))
        self.TotalDuration.setAccessibleName("")
        self.TotalDuration.setAccessibleDescription("")
        self.TotalDuration.setAlignment(QtCore.Qt.AlignCenter)
        self.TotalDuration.setObjectName("TotalDuration")
        self.horizontalLayout_8.addWidget(self.TotalDuration)

        self.CurrentDuration.setText("0:00")
        self.TotalDuration.setText("0:00")
        self.verticalLayout_7.addWidget(self.ProgressBar)
        self.horizontalLayout_2.addWidget(self.PlayerMain)
        self.PlayBarRight = QtWidgets.QFrame(self)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.PlayBarRight.sizePolicy().hasHeightForWidth())
        self.PlayBarRight.setSizePolicy(sizePolicy)
        self.PlayBarRight.setMinimumSize(QtCore.QSize(360, 0))
        self.PlayBarRight.setMaximumSize(QtCore.QSize(360, 16777215))
        self.PlayBarRight.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.PlayBarRight.setFrameShadow(QtWidgets.QFrame.Raised)
        self.PlayBarRight.setObjectName("PlayBarRight")
        self.verticalLayout_10 = QtWidgets.QVBoxLayout(self.PlayBarRight)
        self.verticalLayout_10.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_10.setSpacing(0)
        self.verticalLayout_10.setObjectName("verticalLayout_10")
        self.VolumeBox = QtWidgets.QFrame(self.PlayBarRight)
        self.VolumeBox.setMinimumSize(QtCore.QSize(0, 55))
        self.VolumeBox.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.VolumeBox.setFrameShadow(QtWidgets.QFrame.Raised)
        self.VolumeBox.setObjectName("VolumeBox")
        self.VolumeBoxLayout = QtWidgets.QHBoxLayout(self.VolumeBox)
        self.VolumeBoxLayout.setObjectName("VolumeBoxLayout")
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.VolumeBoxLayout.addItem(spacerItem)
        self.VolumeButton = QtWidgets.QPushButton(self.VolumeBox)
        self.VolumeButton.setMinimumSize(QtCore.QSize(36, 36))
        self.VolumeButton.setMaximumSize(QtCore.QSize(36, 36))
        self.VolumeButton.setText("")
        icon13 = QtGui.QIcon()
        icon13.addPixmap(QtGui.QPixmap(":/icons/images/icon/sound-on_1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.VolumeButton.setIcon(icon13)
        self.VolumeButton.setIconSize(QtCore.QSize(25, 25))
        self.VolumeButton.setObjectName("VolumeButton")
        self.VolumeBoxLayout.addWidget(self.VolumeButton)
        self.verticalLayout_10.addWidget(self.VolumeBox)
        self.OtherBox = QtWidgets.QFrame(self.PlayBarRight)
        self.OtherBox.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.OtherBox.setFrameShadow(QtWidgets.QFrame.Raised)
        self.OtherBox.setObjectName("OtherBox")
        self.OtherBoxLayout = QtWidgets.QHBoxLayout(self.OtherBox)
        self.OtherBoxLayout.setContentsMargins(-1, -1, 8, -1)
        self.OtherBoxLayout.setObjectName("OtherBoxLayout")
        spacerItem1 = QtWidgets.QSpacerItem(293, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.OtherBoxLayout.addItem(spacerItem1)
        self.ChangeMiniButton = QtWidgets.QPushButton(self.OtherBox)
        self.ChangeMiniButton.setMinimumSize(QtCore.QSize(30, 30))
        self.ChangeMiniButton.setMaximumSize(QtCore.QSize(30, 30))
        self.ChangeMiniButton.setText("")
        icon14 = QtGui.QIcon()
        icon14.addPixmap(QtGui.QPixmap(":/icons/images/icon/mini_1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.ChangeMiniButton.setIcon(icon14)
        self.ChangeMiniButton.setIconSize(QtCore.QSize(20, 20))
        self.ChangeMiniButton.setObjectName("ChangeMiniButton")
        self.OtherBoxLayout.addWidget(self.ChangeMiniButton)
        self.verticalLayout_10.addWidget(self.OtherBox)
        self.horizontalLayout_2.addWidget(self.PlayBarRight)

    def initSlider(self):
        self.VolumeSlider = VolumeSlider(Qt.Horizontal, self)
        self.VolumeSlider.setRange(0, 100)
        self.VolumeSlider.setStyle(HollowHandleStyle({"sub-page.color": QColor(70, 23, 180)}))
        self.VolumeSlider.setProperty("value", self.volume)
        self.VolumeSlider.setObjectName("VolumeSlider")
        self.VolumeBoxLayout.addWidget(self.VolumeSlider)

        self.ProgressSlider = ProgressSlider(Qt.Horizontal, self)
        self.ProgressSlider.setStyle(HollowHandleStyle({"sub-page.color": QColor(70, 23, 180)}))
        self.ProgressSlider.setProperty("value", 0)
        self.ProgressSlider.setObjectName("ProgressSlider")
        self.ProgressSliderLayout.addWidget(self.ProgressSlider)

    def initSignal(self):
        self.PlayButton.clicked.connect(CommonSignal.playSignal)
        self.VolumeButton.clicked.connect(CommonSignal.volumeButtonSignal)
        self.VolumeSlider.valueChanged.connect(CommonSignal.volumeChangedSignal)
        self.ProgressSlider.sliderMoved.connect(CommonSignal.progressSliderMovedSignal)
        self.ProgressSlider.clicked.connect(CommonSignal.progressSliderMovedSignal)
        self.NextButton.clicked.connect(CommonSignal.playNextSignal)
        self.PreButton.clicked.connect(CommonSignal.playPreSignal)
        self.ChangeMiniButton.clicked.connect(CommonSignal.miniWindowOpenSignal)
        CommonSignal.changeCoverSignal.connect(self.updateCover)
        CommonSignal.changePlayBarColorSignal.connect(self.changeColor)
        CommonSignal.currentDurationChangeSignal.connect(self.changeCurrentDuration)
        CommonSignal.totalDurationChangeSignal.connect(self.changeTotalDuration)

    def initLastPlaying(self):
        info = LocalMusic.getPlayingInfo(self)
        if info:
            cover = Config.coverPath + info[4] + '-' + info[2] + '.jpg'
            CommonSignal.changeCoverSignal.emit(cover)
            CommonSignal.changeSongInfoSignal.emit(info[1], info[4])
            CommonSignal.totalDurationChangeSignal.emit(info[6])
            r, g, b = DominantColor.getDominantColor(cover)
            color = "rgb("+ str(r) +","+ str(g) +","+ str(b) +")"
        else:
            color = "rgb(34, 92, 127)"
        self.setStyleSheet("#PlayerBar{background-color:"+ color +";}")

    def updateCover(self,cover):
        self.MusicCoverLabel.setPixmap(QtGui.QPixmap(cover))
        # self.MusicCoverLabel.setPixmap(QtGui.QPixmap.fromImage(QImage(cover)))

    def changeColor(self,color):
        self.setStyleSheet("")
        palette = QPalette()
        palette.setColor(self.backgroundRole(), color)
        self.setAutoFillBackground(True)
        self.setPalette(palette)

    def changeCurrentDuration(self, timeFormat):
        self.CurrentDuration.setText(timeFormat)

    def changeTotalDuration(self, timeFormat):
        self.TotalDuration.setText(timeFormat)

    def test(self):
        # self.MusicInfo.updateData()
        print('------test-------')